package com.shuai.jit.security.entity;


import com.shuai.jit.entity.user.User;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 方便程序判断当前请求的用户有什么权限
 *
 * @author bn
 */
@Data
@Slf4j
public class SecurityUser implements UserDetails {

    //当前登录用户
    private transient User currentUserInfo;

    /**
     * 当前权限 字符串
     */

    private List<String> permissionValueList = new ArrayList<>();

    List<GrantedAuthority> authorityList = new ArrayList<>();

    public SecurityUser(User user, List<String> permissionValueList) {
        if (user != null) {
            this.currentUserInfo = user;
        }
        this.permissionValueList = permissionValueList;
        for (String permission : permissionValueList) {
            authorityList.add(new SimpleGrantedAuthority(permission));
        }

    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorityList;
    }

    @Override
    public String getPassword() {
        return currentUserInfo.getPassword();
    }

    /**
     * 用户名为 用户名+salt
     *
     * @return
     */
    @Override
    public String getUsername() {
        return currentUserInfo.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
